package com.rr.sc.bo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rr.sc.bo.CategoryBo;
import com.rr.sc.dao.CategoryDao;
import com.rr.sc.model.Category;
import com.rr.sc.utils.BoImplUtils;

@Service
public class CategoryBoImpl implements CategoryBo {
    private CategoryDao categoryDao;

    @Autowired
    private CategoryBoImpl(CategoryDao categoryDao) {
	this.categoryDao = categoryDao;
    }

    public void saveOrUpdate(Category category) {
	if (category.getParentCategory() != null && (category.getParentCategory().getId() == null || category.getParentCategory().getId().equals(0L))) {
	    category.setParentCategory(null);
	}
	categoryDao.saveOrUpdate(category);
    }

    public Category get(Long id) {
	return categoryDao.get(id);
    }

    public void delete(Category category) {
	categoryDao.delete(category);
    }

    public List<Category> getByUserId(Long userId, boolean onlyRoots) {
	return categoryDao.getByUserId(BoImplUtils.getAsList(userId), onlyRoots);
    }

    public List<Category> getByUserIdAndName(Long userId, String name) {
	return categoryDao.getByUserIdAndName(BoImplUtils.getAsList(userId), name);
    }

    @Override
    public void check(Category category) {
	categoryDao.check(category);
    }
}
